package com.hitplane.manager 
{
	import com.hitplane.debug.DebugTrace;
	import com.hitplane.elements.NormalUserFly;
	import com.hitplane.interfaces.ITick;
	
	import flash.events.Event;
	import flash.utils.getTimer;

	/**
	 * 管理帧频
	 * 单例模式类
	 * @author 郑子华
	 */
	public class EnterFrameManager
	{
		private static var _instance:EnterFrameManager = null;
		public function EnterFrameManager(single:Single) 
		{
			
			if(single == null)
			{
				throw new Error("Can't create instance , Single is Null!");
			}
		}
		/**
		 * 单例引用
		 */
		public static function get instance():EnterFrameManager
		{
			if(_instance == null)
			{
				_instance = new EnterFrameManager(new Single());
			}
			return _instance;
		}
		//start===============================================================
		//需要帧频来触发的对象集合
		private var _relateArr:Array;
		/**
		 * 添加tickObj
		 * @param	tickObj
		 */
		public function addChild(tickObj:ITick):void 
		{
			if (_relateArr == null)_relateArr = [];
			var index:int = _relateArr.indexOf(tickObj);
			if (index == -1) {
				_relateArr.push(tickObj);
			}else {
				DebugTrace.traceObj(this, "要添加的对象已经在列表中");
			}
		}
		/**
		 * 删除列表中的tickObj
		 * @param	tickObj
		 */
		public function removeChild(tickObj:ITick):void {
			if (_relateArr == null) throw new Error(this + "_relateArr为null");
			var index:int = _relateArr.indexOf(tickObj);
			if (index != -1) {
				_relateArr.splice(index, 1);
			}else {
				DebugTrace.traceObj(this, "要删除的对象不在列表中");
			}
		}
		public function startTick():void {
			GameContainManager.instance.mainStage.addEventListener(Event.ENTER_FRAME, updateForTick);
		}
		public function closeTick():void {
			GameContainManager.instance.mainStage.removeEventListener(Event.ENTER_FRAME, updateForTick);
		}
		//private===============================================================
		private function updateForTick(e:Event):void {
			var tickTime:int = getTimer();
//			DebugTrace.traceObj(this,"num of obj:"+_relateArr.length);
			for each (var item:ITick in _relateArr) 
			{
				item.updateForTick(tickTime);
			}
		}
	}
}
class Single{}